package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.model.ActiveSoft;
import com.vm.model.AppType;
import com.vm.model.Soft;
import com.vm.utils.JsonUtils;
import com.vm.activity.BitmapCache;
import com.vm.commons.VMConst;
import com.wwt.app.R;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月16日 下午3:28:11]
 * @Version: [v1.0]
 * 
 */
public class ShakeActivity extends Activity implements SensorEventListener {
	protected static final String TAG = null;
	private ImageLoader mImageLoader;
	SensorManager sensorManager = null;
	Vibrator vibrator = null;
	private List<ActiveSoft> listsoft;
	private RequestQueue mQueue;
	private ImageView iv_userImage;
	private TextView tv_userName;
	private LinearLayout f_item_ll;
	private ImageView iv_waveImage;
	ImageView imgbtn_search_back;
	private String softId;
	private static boolean isReady = true;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wave);

		init();
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		listsoft = new ArrayList<ActiveSoft>();

		// iv_waveImage.setOnClickListener(imgClickListener);
		mQueue = Volley.newRequestQueue(this);
		String url = VMConst.REQUEST_URL + "getByActiveType?typeId=" + 19;
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {

					@Override
					public void onResponse(JSONArray arg0) {
						// TODO Auto-generated method stub
						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								// String s=jsonObject.toString();

								ActiveSoft activesoft = JsonUtils.jsonToBean(
										jsonObject, ActiveSoft.class);
								listsoft.add(activesoft);

							} catch (JSONException e) {
								e.printStackTrace();
							}
						}

					}

				}, new Response.ErrorListener() {
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});
		mQueue.add(jsonObjectRequest);

	}

	protected void onPause() {
		super.onPause();
		sensorManager.unregisterListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// 当传感器精度改变时回调该方法，Do nothing.
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		int sensorType = event.sensor.getType();
		// values[0]:X轴，values[1]：Y轴，values[2]：Z轴
		float[] values = event.values;
		if (sensorType == Sensor.TYPE_ACCELEROMETER) {
			if ((Math.abs(values[0]) > 19 || Math.abs(values[1]) > 19 || Math
					.abs(values[2]) > 19) && isReady) {
				isReady = false;
				
				Log.d("sensor x ", "============ values[0] = " + values[0]);
				Log.d("sensor y ", "============ values[1] = " + values[1]);
				Log.d("sensor z ", "============ values[2] = " + values[2]);
				f_item_ll.setVisibility(View.VISIBLE);
				ActiveSoft activeSoft = listsoft
						.get((int) (Math.random() * listsoft.size()));
				RequestQueue requestQueue = Volley
						.newRequestQueue(ShakeActivity.this);
				mImageLoader = new ImageLoader(requestQueue, new BitmapCache());
				ImageListener listener = ImageLoader.getImageListener(
						iv_userImage, android.R.drawable.ic_menu_rotate,
						android.R.drawable.ic_delete);
				mImageLoader.get(VMConst.URL + activeSoft.getImageUrl(),
						listener);
				tv_userName.setText(activeSoft.getName());
				f_item_ll.setOnClickListener(ll_listener);
				softId = activeSoft.getSoftId();
				vibrator.vibrate(500);
				
				
				isReady = true;
			}

		}

	}

	OnClickListener ll_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(ShakeActivity.this, DetailActivity.class);
			intent.putExtra("softId", softId);
			startActivity(intent);
		}
	};

	/**
	 * 初始化
	 */
	private void init() {

		iv_userImage = (ImageView) findViewById(R.id.iv_userImage);
		tv_userName = (TextView) findViewById(R.id.tv_userName);
		f_item_ll = (LinearLayout) findViewById(R.id.f_item_ll);
		iv_waveImage = (ImageView) findViewById(R.id.iv_waveImage);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

}
